package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domain.User;
import com.example.result.UserResult;
import com.example.result.JsonResult;
import com.example.service.UserService;
import com.example.untils.MD5encrypt;
import com.example.untils.RedisContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public JsonResult login(@RequestBody User user,HttpSession session){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("is_delete",0);
        User result = userService.getOne(wrapper);
        if(result==null){
            throw new RuntimeException("用户不存在");
        }
        //获取盐值
        String salt=result.getSalt();
        String md5Password = MD5encrypt.getMD5Password(user.getPassword(), salt);   //md5Password密码MD5加密后的值
        if(!result.getPassword().equals(md5Password)){
            throw new RuntimeException("密码不正确");
        }
        UserResult userResult=new UserResult(result);

        session.setAttribute("user",userResult);

        return new JsonResult(true,"登录成功");
    }
    //邮箱登录
    @PostMapping("/loginEmail")
    public JsonResult loginEmail(@RequestBody User user){
        if(true){
            throw new RuntimeException("本服务为开启");
        }

        return new JsonResult(true,"登录成功");
    }
    //获取redis中的user
    @GetMapping("/getUser")
    public JsonResult select(HttpSession session){
        UserResult userResult = getSessionUser(session);
        if(userResult==null){
            throw new RuntimeException("获取失败");
        }
        return new JsonResult(true,"",userResult);
    }
    //注销用户
    @GetMapping("/logout")
    public ModelAndView logout(HttpSession session){
        session.removeAttribute("user");
        ModelAndView model=new ModelAndView("redirect:/login/index.html");
        return model;
    }
    //注册新用户
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody User user){
        QueryWrapper<User> wrapper1=new QueryWrapper<>();
        wrapper1.eq("username",user.getUsername());
        wrapper1.eq("is_delete",0);
        User result1 = userService.getOne(wrapper1);
        if(result1!=null){
            throw new RuntimeException("用户名已存在,请更换!");
        }
        QueryWrapper<User> wrapper2=new QueryWrapper<>();
        wrapper2.eq("email",user.getEmail());
        wrapper2.eq("is_delete",0);
        User result2 = userService.getOne(wrapper2);
        if(result2!=null){
            throw new RuntimeException("邮箱已被绑定,请更换!");
        }
        //密码加密
        String salt= UUID.randomUUID().toString().toUpperCase();   //随机生成盐值
        String md5Password= MD5encrypt.getMD5Password(user.getPassword(),salt);   //MD5加密
        user.setPassword(md5Password);user.setSalt(salt);
        //日志记录
        user.setCreated_user(user.getUsername());user.setCreated_time(new Date());
        user.setIs_delete(0);
        boolean flag = userService.save(user);
        if(!flag){
            throw new RuntimeException("注册失败,请重试!");
        }
        return new JsonResult(true,"注册成功");
    }
    @GetMapping("/searchUser")
    public JsonResult searchUser(String searchData){
        if(searchData==null||"".equals(searchData)){
            throw new RuntimeException("搜索内容不能为空");
        }
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.like("username",searchData)
            .or().like("phone",searchData).or().like("email",searchData);
        List<User> list = userService.list(wrapper);
        List<UserResult> userResults=new ArrayList<>();
        for (User user : list) {
            userResults.add(new UserResult(user));
        }
        return new JsonResult(true,"",userResults);
    }
    //根据uid查找用户信息
    @GetMapping("/getUserID")
    public JsonResult getUserID(Long uid){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",uid);
        User user = userService.getOne(wrapper);
        if(user==null){
            throw new RuntimeException("此用户数据不存在");
        }
        UserResult userResult = new UserResult(user);
        return new JsonResult(true,"",userResult);
    }
}
